//
//  GuidePageContentView.swift
//  sss
//
//  Created by MAC on 2020/12/24.
//  Copyright © 2020 hrrMac. All rights reserved.
//

import UIKit

class GuidePageContentView: UIView {

    ///图片
    private let imgView = UIImageView()
    ///大标题
    private let titleLab = UILabel()
    //小标题
    private let subTitleLab = UILabel()
    
    //分页控制器
    private let pageControl = UIPageControl()

    // 点击回调
    var jumpBlock :(()->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func configUI(){
        addSubview(imgView)
        addSubview(titleLab)
        addSubview(subTitleLab)
        addSubview(pageControl)
        
        imgView.contentMode        = .scaleAspectFill
        imgView.clipsToBounds      = true
        
        titleLab.font              = UIFont.systemFont(ofSize: 22)
        titleLab.textColor         = UIColor.black
        titleLab.textAlignment     = .center
        
        subTitleLab.font           = UIFont.systemFont(ofSize: 16)
        subTitleLab.textColor      = UIColor.init(r: 153, g: 153, b: 153)
        subTitleLab.textAlignment  = .center
        
        pageControl.currentPageIndicatorTintColor = UIColor.init(r: 59, g: 146, b: 147)  //当前选中原点颜色
        pageControl.pageIndicatorTintColor = UIColor.init(r: 153, g: 153, b: 153)  //未选中圆点颜色
        pageControl.hidesForSinglePage = true //只有一个控制器的时候是否隐藏圆点
        
        let h = self.frame.size.height;
        let w = self.frame.size.width;
        
        ///调整图片高度，默认为 左右间距 10pt  高度为视图高度的 2/3 - 60
        imgView.frame = CGRect.init(x: 10, y: 60, width: w - 10 * 2, height: 2 * h / 3.0 - 60)
        
        titleLab.frame = CGRect.init(x: 10, y: 2 * h / 3.0, width: w - 10 * 2, height: 60)
        
        subTitleLab.frame = CGRect.init(x: 10, y: 2 * h / 3.0 + 60, width: w - 10 * 2, height: 40)
        
        pageControl.center = CGPoint.init(x: self.frame.size.width / 2, y: self.frame.size.height - 40)

    }
    
    
    private func jumpNextEvent(){
        let btn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 44))
        btn.center = CGPoint.init(x: self.frame.size.width / 2, y: self.frame.size.height - 100)
        btn.layer.cornerRadius = 6
        btn.backgroundColor = UIColor.init(r: 59, g: 146, b: 147)
        btn.setTitle("即可体验", for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        
        addSubview(btn)
        btn.addTarget(self, action: #selector(clickNextEvent), for: .touchUpInside)
    }
    
    
    @objc func clickNextEvent() {
        if(jumpBlock != nil){
            jumpBlock!()
        }
    }
    
    func  refreshData(_ imgStr:String,
                      title: String ,
                      subTitle: String ,
                      pageIndex: Int ,
                      allPage: Int) {
        imgView.image = UIImage.init(named: imgStr)
        titleLab.text = title
        subTitleLab.text = subTitle
        pageControl.numberOfPages = allPage
        pageControl.currentPage = pageIndex
        if(pageIndex + 1 == allPage){
            jumpNextEvent()
        }
        
    }
    
    deinit {
        print("-----------deinit" + self.debugDescription + "-----------------")
    }
    
}
